package com.panshi.service.impl;

import com.panshi.mapper.IQuestionAuditMapper;
import com.panshi.mapper.IQuestionMapper;
import com.panshi.service.IQuestionAuditService;
import com.panshi.vo.qa.Pages;
import com.panshi.vo.qa.Question;
import com.panshi.vo.qa.QuestionAudit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/*
 *问题审核
 */
@Service
public class QuestionAuditService implements IQuestionAuditService {

    @Autowired
    IQuestionAuditMapper questionAuditMapper;

    @Autowired
    IQuestionMapper iQuestionMapper;

    @Override
    @Transactional(propagation= Propagation.REQUIRED)//spring事务注解型
    public boolean addQuestionAudit(QuestionAudit questionAudit) {
        try {
            questionAuditMapper.addQuestionAudit(questionAudit);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    public Pages<QuestionAudit> inquirePagesQuestionAudit(Pages pages) {
        List<QuestionAudit> list = questionAuditMapper.inquirePagesQuestionAudit(pages);
        Integer sum= questionAuditMapper.inquireQuestionAuditSum(pages);
        Pages<QuestionAudit> p = new Pages<>();
        p.setList(list);
        p.setSum(sum);
        return p;
    }

    @Override
    public Integer inquireQuestionAuditSum(Pages pages) {
        Integer sum= questionAuditMapper.inquireQuestionAuditSum(pages);
        return sum;
    }

    @Override
    @Cacheable(value = "questionAudit:inquireById",key = "#id")
    public QuestionAudit inquireById(int id) {
        QuestionAudit questionAudit = questionAuditMapper.inquireById(id);
        return questionAudit;
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED)//spring事务注解型
    @CacheEvict(value = "questionAudit:inquireById",key = "#p0.q_id")
    public boolean updateQuestionAudit(QuestionAudit questionAudit) {
        try {
            questionAuditMapper.updateQuestionAudit(questionAudit);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED)//spring事务注解型
    @CacheEvict(value = "questionAudit:inquireById",key = "#id")
    public boolean deleteQuestionAudit(int id) {
        try {
            questionAuditMapper.deleteQuestionAudit(id);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED)//spring事务注解型
    @CacheEvict(value = "questionAudit:inquireById",key = "#p0.q_id")
    public boolean updateState(QuestionAudit questionAudit) {
        try {
            questionAuditMapper.updateState(questionAudit);
            //a_state == 2 说明是审核通过的直接添加到 问题表 展示
            if (questionAudit.getQState() == 2){

                Integer qid = questionAudit.getQId();
                QuestionAudit qa = questionAuditMapper.inquireById(qid);

                //创建问题
                Question question = new Question();
                //内容
                String content = qa.getQContent();
                question.setQContent(content);
                //摘要
                String qAbstract=qa.getQAbstract();
                question.setQAbstract(qAbstract);
                //图片路径
                String url = qa.getQImgUrl();
                question.setQImgUrl(url);
                //所属用户
                int userId = qa.getQUserId();
                question.setQUserId(userId);
                //分类id
                int titleId = qa.getQTitleId();
                question.setQTitleId(titleId);
                //创建时间
                String createDate = qa.getQCreateDate();
                question.setQCreateDate(createDate);
                //添加问题
                iQuestionMapper.addQuestion(question);

            }
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    public List<QuestionAudit> inquireMyQuestionAudit(int userId) {
        List<QuestionAudit> list = questionAuditMapper.inquireMyQuestionAudit(userId);
        return list;
    }
}
